<template>
  <div class="page">
    <BasicTable @register="registerTable" 
      :rowSelection="rowSelection"
      :rowKey="record => record.id"
      :showIndexColumn="true"
      :columns="tableColumns"
      :useSearchForm="true">
      <template #toolbar>
        <a-button type="default" @click="handleBatchUse">批量使用</a-button>
        <a-button type="default" @click="handleBatchLock">批量锁定</a-button>
        <a-button type="default" @click="handleBatchCancel">批量退订</a-button>
        <a-upload
          :showUploadList="false"
          :before-upload="() => false"
          @change="handleImport"
          accept=".xlsx,.xls"
        >
          <a-button type="primary">X号码导入</a-button>
        </a-upload>
      </template>
      <template #action="{ record }" >
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: handleEdit.bind(null, record)
            },
            {
              icon: 'ant-design:unlock-outlined',
              tooltip: '锁定',
              onClick: handleLock.bind(null, record)
            },
            {
              icon: 'ant-design:stop-outlined',
              tooltip: '退订',
              onClick: handleCancel.bind(null, record)
            },
            {
              icon: 'ant-design:delete-outlined',
              tooltip: '删除',
              color: 'error',
              popConfirm: {
                title: '是否删除该条记录？',
                confirm: handleRemove.bind(null, record)
              }
            }

          ]"
        />
      </template>
    </BasicTable>
    <EditDrawer @register="registerEditDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  /**
   * X号码表
   * @date 2025-08-20
   * @author exchenming
   * @Email 276490943@qq.com
   */
  import { message } from 'ant-design-vue'
  import { useTable } from '@/components/Table'
  import { useDrawer } from '@/components/Drawer'
  import EditDrawer from './NoQsDrawer.vue'
  import { listNoQs, removeNoQs,lockNoQs,cancelNoQs,uploadNoQsExcel,getCityByProvince,batchCancelNoQs,batchLockNoQs,batchUseNoQs } from '@/api/x/noQs'
  import { tableColumns, searchFormSchema } from './noQs.data'
  import * as XLSX from 'xlsx'
  import type { UploadChangeParam } from 'ant-design-vue'
  import { useTabs } from '@/hooks/web/useTabs'
  

  const [registerTable, { reload: reloadList,getSelectRows }] = useTable({
    title: 'X号码表列表',
    titleHelpMessage: '列表',
    api: (params) => {
    // 处理 RangePicker 参数，避免 openTime[] 这种格式
  
    console.info(params.openTime)
    if (params.openTime && Array.isArray(params.openTime) && params.openTime.length === 2) {
      params.openStartTime = params.openTime[0]
      params.openEndTime = params.openTime[1]
      delete params.openTime
    }
    if (params.isLock && '全部' === params.isLock) {
      delete params.isLock
    }
    if (params.isCancel && '全部' === params.isCancel) {
      delete params.isCancel
    }
    if (params.isUse && '全部' === params.isUse) {
      delete params.isUse
    }
    if (params.xNo && '' === params.xNo) {
      delete params.xNo
    }
    return listNoQs(params)
  },
    rowKey: 'id',
    columns: tableColumns,
    useSearchForm: true,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema
    },
    showIndexColumn: true,
    actionColumn: {
      width: 120
    }
  })

  const [registerEditDrawer, { openDrawer: openEditDrawer }] = useDrawer()

  const handleCreate = () => {
    openEditDrawer(true, { isUpdate: false })
  }

  const handleEdit = (record: any) => {
    openEditDrawer(true, { record, isUpdate: true })
  }

  const handleRemove = ({ id }) => {
    removeNoQs({ id }).then((res) => {
      if (res.code === 0) {
        message.success('删除成功！')
        reloadList()
      }
    })
  }
  const handleLock = ({ id ,isLock}) => {
    lockNoQs({ id }).then((res) => {
      if (res.code === 0) {
        console.info(isLock)
        if('Y' === isLock){
          message.success('解锁成功！')
        } else {
          message.success('锁定成功！')
        }
        reloadList()
      }
    })
  }
  const handleCancel = ({ id,isCancel }) => {
    cancelNoQs({ id }).then((res) => {
      if (res.code === 0) {
        if('Y' === isCancel){
          message.success('解除退订成功！')
        } else {
          message.success('退订成功！')
        }
        reloadList()
      }
    })
  }

  const handleSuccess = () => {
    reloadList()
  }

  const handleBatchUse = () => {
  const selectedRows = getSelectRows()
  if (!selectedRows.length) {
    message.warning('请至少选择一项')
    return
  }
  const ids = selectedRows.map(item => item.id)
  batchUseNoQs({ ids}).then(res => {
    if (res.code === 0) {
      message.success(res.msg)
      reloadList()
    } else {
      message.error(res.msg)
    }
  })
}

const handleBatchLock = () => {
  const selectedRows = getSelectRows()
  if (!selectedRows.length) {
    message.warning('请至少选择一项')
    return
  }
  const ids = selectedRows.map(item => item.id)
  batchLockNoQs({ ids }).then(res => {
    if (res.code === 0) {
      message.success(res.msg)
      reloadList()
    } else {
      message.error(res.msg)
    } 
  })
}

const handleBatchCancel = () => {
  const selectedRows = getSelectRows()
  if (!selectedRows.length) {
    message.warning('请至少选择一项')
    return
  }
  const ids = selectedRows.map(item => item.id)
  batchCancelNoQs({ ids }).then(res => {
    if (res.code === 0) {
      message.success(res.msg)
      reloadList()
    } else {
      message.error(res.msg)
    }
  })
}

const handleImport = (info: UploadChangeParam) => {
    uploadNoQsExcel({ file: info.file }).then((res) => {
      if (res.code === 0) {
        message.success(res.msg)
      } else message.warning(res.data.msg)
    })
  }

</script>